const MongoClient = require('mongodb').MongoClient
const url = 'mongodb://localhost:27017/foxanddog'

const express = require('express')
const app = express()
let options=null
// 允许所有的请求形式
app.use(function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
  next()
})
MongoClient.connect(url).then(conn => {
 options = conn.db('foxanddog').collection('options')
}).catch((err) => {
  console.log('数据库连接失败：', err.message)
})
var server = app.listen(8081, 'localhost', () => {
  var host = server.address().address
  var port = server.address().port
  console.log('应用实例，访问地址为 http://%s:%s', host, port)
})
//查询下拉框字典
app.get('/getDataDictionary',function (req,res){
  options.find({}, {projection: {name: 1, value: 1}}).toArray().then(result=>{
    console.log(result)
    res.send(result)
  })
})
//查询下拉框数据
app.get('/getSelectOption', function (req, res) {
  options.find(req.query).toArray().then(result => {
    res.send({result})
  })
})

